package com.mhwx.mhlive.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mhwx.mhlive.R;
import com.mhwx.mhlive.bean.CNMessageInfo;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;



/**
 * 类功能描述
 *
 * @author Guol
 * @version 1.0
 * @copyright 北京明华维新信息技术有限公司
 * @time 2017/5/5
 */
public class ChatListAdapter extends BaseQuickAdapter<CNMessageInfo> {
     private Context context;
    private LayoutInflater mInflater = null;
    private final RelativeLayout.LayoutParams layoutParams;
    public ChatListAdapter(Context context, List<CNMessageInfo> data) {
        super(R.layout.item_medical_char, data);
        mInflater = LayoutInflater.from(context);
        layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected void convert(BaseViewHolder baseViewHolder, CNMessageInfo cnMessageInfo) {
        if (cnMessageInfo.isSendMesageInfo()) {
            baseViewHolder.setText(R.id.msg_text,cnMessageInfo.getCnMessage().getUserInfo().getUserName() == null ? "" : " : " + cnMessageInfo.getCnMessage().getUserInfo().getUserName());
            try {
                baseViewHolder.setText(R.id.user_name,cnMessageInfo.getCnMessage().getContent() == null ? "" :  (String) new JSONObject(cnMessageInfo.getCnMessage().getContent()).get("message"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_START);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_END);
            baseViewHolder.getView(R.id.re_chat_room).setLayoutParams(layoutParams);
            baseViewHolder.setTextColor(R.id.msg_text, Color.parseColor("#d61d0c"));
            baseViewHolder.setTextColor(R.id.user_name, Color.BLACK);
        } else {
            baseViewHolder.setText(R.id.user_name,cnMessageInfo.getCnMessage().getUserInfo().getUserName() == null ? "" : cnMessageInfo.getCnMessage().getUserInfo().getUserName() + " : ");
            try {
                baseViewHolder.setText(R.id.msg_text,cnMessageInfo.getCnMessage().getContent() == null ? "" : cnMessageInfo.getCnMessage().getContent() == null ? "" :  (String) new JSONObject(cnMessageInfo.getCnMessage().getContent()).get("message"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
            baseViewHolder.getView(R.id.re_chat_room).setLayoutParams(layoutParams);
            layoutParams.removeRule(RelativeLayout.ALIGN_PARENT_END);
            layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
            baseViewHolder.setTextColor(R.id.user_name, Color.parseColor("#d61d0c"));
            baseViewHolder.setTextColor(R.id.msg_text, Color.BLACK);
        }
    }




}
